#!/usr/bin/env python3

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask import render_template
from flask import request
 


# 实例化
app = Flask(__name__)

# 连接到数据库
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+pymysql://root:111111@localhost/test"
db = SQLAlchemy(app)

#定义类映射到test.stu
class stuInfo(db.Model):
    __tablename__ = 'stu'
    id = db.Column(db.Integer, primary_key=True)
    sex = db.Column(db.String)
    age = db.Column(db.Integer)
    num = db.Column(db.String, unique=True)
    address = db.Column(db.String)
    grade = db.Column(db.Integer)
    sub = db.Column(db.String)
    name = db.Column(db.String, unique=True)

@app.route('/')
def v_index():
    return render_template('stuinfo.html')
#    return 'hello world! i am DT'

@app.route('/addinfo',methods=["POST"])
def addinfo():
    stu_info = stuInfo(id=request.form['id'],sex=request.form['sex'],age=request.form['age'],num=request.form['num'],address=request.form['address'],grade=request.form['grade'],sub=request.form['sub'],name=request.form['name'])
    db.session.add(stu_info)
    db.session.commit()
    return '信息插入成功'



if __name__ == '__main__':
    app.run(host='0.0.0.0',port=8088,debug=True,threaded=True)
